//
//  IndexTableViewController.m
//  ksjv2
//
//  Created by mac on 15/1/7.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "IndexTableViewController.h"
#import "Util.h"
#import "ArticleViewController.h"

@interface IndexTableViewController (){
    NSMutableDictionary*listDit;
}

@end

@implementation IndexTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"";
    [SVProgressHUD showWithStatus:@"数据加载中..." maskType:SVProgressHUDMaskTypeClear];
    listDit=[[NSMutableDictionary alloc]init];
    [self loadNews];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
-(void)loadNews{
    
    NSString *openUdid = [OpenUDID_UDID value];
    NSLog(@"openUdid == %@",openUdid);
    
    
    [[AppAPIClient sharedClient] GET:@"Magazine/Index" parameters:@{@"ios":@"1",@"magid":self.Idict[@"magid"],@"id":self.Idict[@"parentid"],@"openudid":openUdid} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *result = responseObject[@"data"];
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        
        listDit=responseObject[@"data"];


        
//        for (NSDictionary*dic in result) {
//            if ([listDict objectForKey:dic[@"category"]]) {
//                NSMutableArray*array=[listDict objectForKey:dic[@"category"]];
//                [array addObject:dic];
//            }else{
//                NSMutableArray*array=[[NSMutableArray alloc]init];
//                [array addObject:dic];
//                [listDict setObject:array forKey:dic[@"category"]];
//            }
//            
//        }
//        if (listArray==nil) {
//            
//            listArray=[[NSMutableArray alloc]initWithArray:responseObject[@"data"] copyItems:YES];
//        }else{
//            NSMutableArray *newResult = [[NSMutableArray alloc]initWithArray:listArray];
//            [newResult addObjectsFromArray:responseObject[@"data"]];
//            listArray = [[NSMutableArray alloc]initWithArray:newResult copyItems:YES];
//        }
        
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ERR: %@",operation.responseString);
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger nuber=[listDit[@"category"] count];
    return nuber;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = listDit[@"category"][section];
    return [listDit[@"list"][key] count];
//    NSString *key=[listDit allKeys][section];
//    NSArray*array=listDit[key];
//    NSInteger number=[array count];
//    return number;
//    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 10;
////}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    NSString *key=listDit[@"category"][section];
//    return key;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSString *key=listDit[@"category"][section];
    UILabel *lab = [[UILabel alloc]init];
    [lab setText:[NSString stringWithFormat:@"   %@",key]];
    lab.backgroundColor=[UIColor colorWithWhite:0.890 alpha:0.800];
    lab.font=[UIFont systemFontOfSize:19];
    lab.frame=CGRectMake(0, 0, tableView.frame.size.width, 60);
    return lab;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell==nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    }
//    NSLog(@"section%d___row%d",indexPath.section,indexPath.row);

    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSString *key=listDit[@"category"][indexPath.section];
    NSArray*array=listDit[@"list"][key];
    
    NSDictionary*dic=array[indexPath.row];
    UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(15, 11, self.view.frame.size.width-20, 22)];
    title.text=dic[@"title"];
    [cell.contentView addSubview:title];
    UILabel*description=[[UILabel alloc]initWithFrame:CGRectMake(15, 38, self.view.frame.size.width-20, 22)];
    description.font=[UIFont systemFontOfSize:14];
    description.textColor=[UIColor lightGrayColor];
    description.text=dic[@"description"];
    [cell.contentView addSubview:description];
//    UIImageView*imageview=(UIImageView*)[cell.contentView viewWithTag:11];
//    [imageview sd_setImageWithURL:dic[@"thumb"] placeholderImage:[UIImage imageNamed:@"icon_default_ipad"]];

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ArticleViewController*art=[story instantiateViewControllerWithIdentifier:@"ArticleViewController"];
        NSString *key=listDit[@"category"][indexPath.section];
        NSArray*array=listDit[@"list"][key];
        NSDictionary*dic=array[indexPath.row];
        art._Dict=dic;
        art.isCommentVC=NO;
        art.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:art animated:YES completion:nil];
    
}

@end
